"""Simple XML wraper
Usage:    
    import s3dxml

    to load file use:
        s = s3dxml.load(namefile)
    
    to find node use: 
        node = xml.getNode("main/perforce/art")
        node = node.getNode("nameClient")
    
    to get value use:
        val = node.getValue()
"""

from xml.dom import minidom

class s3dXML:
   def __init__ (self, name = ""):
      if name == "":
         return 
            
      try:    
         self.node = minidom.parse(name)   
      except Exception, err:    
         raise

   def __str__ (self):
      return self.node.toxml()

   def toxml (self):
      return self.node.toxml()

   def getXmlNode(self):
      return self.node

   def getName (self):
      return self.node.nodeName

   def getNodeValue (self):
      return self.node.firstChild.nodeValue

   #s3dXML
   def getNodeInternal (self, xmldoc, key):
      keyPartition = key.partition("/")

      if xmldoc.nodeName != keyPartition[0]:
         return None
    
      if keyPartition[2] == "":
         return xmldoc
        
      for node in xmldoc.childNodes:
         nodeFind = self.getNodeInternal(node, keyPartition[2])
         if nodeFind:
            return nodeFind
            
      return None

   def getNode(self, key):
      node = self.getNodeInternal(self.node, self.node.nodeName + "/" + key)
      if not node:
         return None
            
      xml = s3dXML()
      xml.node = node
      return xml

   def getValue (self, key):
      node = self.getNode(key)
      if not node:
         return None
      return node.getNodeValue()

   def getAttributeValue(self, key):
      """ return attribute value by a given key in a xx/xx/attrname format"""
      keyPartition = key.rpartition("/")
      
      # find child node if any
      if keyPartition[0] != "":
         node = self.getNode(keyPartition[0]).node
      else:
         node = self.node
      
      if not node:
         return None
      
      if not node.hasAttributes:
         return None
      
      attributes = node.attributes
      for i in range(attributes.length):
         attr = attributes.item(i)
         if attr.nodeName == keyPartition[2]:
            return attr.nodeValue
            
      return None

   def getChildNodes(self):
      childNodes = []
      for node in self.node.childNodes:
         xmlNode = s3dXML()
         xmlNode.node = node
         childNodes.append(xmlNode)
      return childNodes

   #service
   def saveDocumentStandart(self, name):
      try:
         outputfile = open(name, "w")
         self.node.writexml(outputfile, "", "\t", "\n")
      except: 
         raise

   def saveDocument(self, name):
      try:
         import s3dxml2vcproj
         s3dxml2vcproj.saveDocument(name, self.node)
      except: 
         raise

def load (name):
   try:
      s = s3dXML(name) 
   except: 
      raise
      return s

   return s
   
if __name__ == "__main__":
   print __doc__